<?php
if(!defined('BASE_DIR'))die('Cannot access the file !');
/**
 * -+-----------------------------------------
 * |PHP Framework
 * |Copyright (c) 2010 jinke.[iblue.cc]
 * |E-mail: mejinke@gmail.com
 * |Date: 2011-08-13
 * -+-----------------------------------------
 *
 * MongoDB数据库驱动
 * @author jinke
 */
class MongoDB extends Base
{
	//结果集
	private $result;
	//数据库名称
	public $dbName='';
	
	
	//连接标识
	private $hostConnection = null;
	private $dbConnection = null;
	
	//连接字符串
	private $connection_string ='';

	//数据库资料数组
	public $dbConfig = '';

	//事务标识
	private $transTimes = 0;
	
	public $show_sql = false;
	
	/**
	 * 构造函数，初始化配置文件
	 * @access public
	 * @param array $db_connect 数据配置资料
	 */
	public function __construct(Array $dbConfig,$dbName)
	{
		
		if(!class_exists('Mongo'))
		{
			message_error('MongoDB Error', 'MongoDB的PECL扩展尚未安装或启用');
		}else 
		{	
			$dbConfig['dbname']=$dbName;
			$this->dbConfig = $dbConfig;
			//数据库名称
			$this->dbName = $this->dbConfig['dbname'];
			$this->getConnectString();
			
		}
		Debug::initClass('mongodb.class.php host=>'.$this->dbConfig['hostname'].' database=>'.$dbName);
	}

	
	/**
	 * 生成连接数据库的字符串
	 * @access private
	 */
	private function getConnectString()
	{
		$connection_string = "mongodb://";
		
		if(!empty($this->dbConfig['username']) && !empty($this->dbConfig['password']))
		{
			$connection_string .= $this->dbConfig['username'].':'.$this->dbConfig['password'].'@';
		}
		
		if(isset($this->dbConfig['port']) && !empty($this->dbConfig['port']))
		{
			$connection_string .= $this->dbConfig['hostname'].':'.$this->dbConfig['port'];
		}
		else
		{
			$connection_string .= $this->dbConfig['hostname'];
		}
		$this->connection_string = trim($connection_string);
	}
	
	/**
	 * 连接MongoDB数据库
	 * @access private
	 * @return void
	 */
	private function _connect()
	{
		if (is_null($this->hostConnection))
		{ 
			try 
			{
				$this->hostConnection = new Mongo($this->connection_string);
			}
			catch (MongoConnectionException $e)
			{
				message_error('Connection Mongo Error',$e->getMessage());
			}
			$this->dbConnection = $this->hostConnection->{$this->dbName};
		}
	}
	
	/**
	 * 查询内容
	 * @access publice
	 * @param string $collection 集合名称
	 * @param array $find 查询的字段
	 * @param array $where 条件数组
	 * @param array $limit 偏移量
	 * @param array $order 排序
	 */
	public function select($collection, $find = array(), $where = array(), $limit = array(), $order = array())
	{	
		$this->_connect();
		$limit_start = 9999999999;
		$limit_offset = 0;
		if (isset($limit['start']))
			$limit_start = $limit['start'];
		if (isset($limit['offset']))
			$limit_offset = $limit['offset'];

		$results = array();
	 	$documents = $this->dbConnection->{$collection}->find($where, $find)
					 	->sort($order)
					 	->limit((int)$limit_offset)
					 	->skip((int)$limit_start);
	 	$returns = array();
	 	
	 	foreach($documents as $doc):
	 		$returns[] = $doc;
	 	endforeach;
	 	
	 	return($returns);
	}
	
	
	/**
	 * 内容更新
	 * @access public
	 * @param string $collection 集合名称
	 * @param array $where 条件数组
	 * @param arra $data 数据
	 */
	public function update($collection, $where = array(), $data = array(),$multiple = true)
	{
		$this->_connect();
		return $this->dbConnection->{$collection}->update($where, array('$set' => $data), array('fsync' => TRUE, 'multiple' => $multiple));
	}
	public function updateNotSet($collection, $where = array(), $data = array())
	{
		$this->_connect();
		return $this->dbConnection->{$collection}->update($where, $data);
	}
	
	
	/**
	 * 添加资料
	 * @access public
	 * @param string $collection 集合名称
	 * @param array $data
	 */
	public function insert($collection, $data = array())
	{
		$this->_connect();
		$this->dbConnection->{$collection}->insert($data, array('fsync' => TRUE));
		if (isset($data['_id']))
			return $data['_id']->{'$id'};
		return false;
	}
	
	/**
	 * 删除记录
	 * @param string $collection 集合名称
	 * @param array $where
	 * @param bool $justOne 是否只删除一条？
	 */
	public function remove($collection, Array $where,$justOne = false)
	{
		$this->_connect();
		return $this->dbConnection->{$collection}->remove($where, array('fsync' => TRUE, 'justOne' => $justOne));
	}
	
	
	public function getCount($collection, Array $where = array())
	{
		$this->_connect();
		return  $this->dbConnection->{$collection}->count($where);
	}
	
	
	public function __destruct()
	{
		if (!is_null($this->hostConnection))
			$this->hostConnection->close();
	}

	
}
?>